import { Auths } from '@/utils/types.d';
import { message } from 'antd';
import type { CheckboxChangeEvent } from 'antd/lib/checkbox';

export const onChange = (list: any[], authVariable: AuthVariable, flag?: string) => {
  let tempList = list || [];

  if (flag === 'systemConfig') {
    let pda = !list.includes(Auths.PDA_READ) && list.includes(Auths.PDA_CONFIG);
    let stats = !list.includes(Auths.STATS_READ) && list.includes(Auths.STATS_CONFIG);
    let ad = !list.includes(Auths.AD_READ) && list.includes(Auths.AD_CONFIG);
    let abo = !list.includes(Auths.ABO_READ) && list.includes(Auths.ABO_CONFIG);
    let order = !list.includes(Auths.ORDER_READ) && list.includes(Auths.ORDER_CONFIG);
    let template = !list.includes(Auths.TEMPLATE_READ) && list.includes(Auths.TEMPLATE_CONFIG);
    if (pda || stats || ad || abo || template || order) {
      message.warn({ content: '无法在无 [查看] 权限时，操作模块', key: 'read_error' });
      tempList = list.filter((item: string) => {
        if (pda && stats && ad)
          return (
            item !== Auths.PDA_CONFIG && item !== Auths.STATS_CONFIG && item !== Auths.AD_CONFIG
          );
        if (pda) return item !== Auths.PDA_CONFIG;
        if (stats) return item !== Auths.STATS_CONFIG;
        if (ad) return item !== Auths.AD_CONFIG;
        if (abo) return item !== Auths.ABO_CONFIG;
        if (template) return item !== Auths.TEMPLATE_CONFIG;
        if (order) return item !== Auths.ORDER_CONFIG;
        return true;
      });
    }
  } else if (!list.includes(Auths.READ)) {
    message.warn({ content: '无法在无 [查看] 权限时，操作模块', key: 'read_error' });
    authVariable.setEnable(true);
    tempList = [];
  } else {
    authVariable.setEnable(false);
  }
  authVariable.setList(tempList);
  authVariable.setIntermin(!!tempList.length && tempList.length < authVariable.auth.length);
  authVariable.setCheckAll(tempList.length === authVariable.auth.length);
};

export const onCheckAllChange = (e: CheckboxChangeEvent, authVariable: AuthVariable) => {
  authVariable.setList(
    e.target.checked ? authVariable.auth.map((item: { value: any }) => item.value) : [],
  );
  authVariable.setIntermin(false);
  authVariable.setEnable(!e.target.checked);
  authVariable.setCheckAll(e.target.checked);
};

export const simplyInit = (authVariable: AuthVariable, dataList: Auths[] = []) => {
  if (dataList.length === 0) {
    authVariable.setIntermin(false);
    authVariable.setEnable(true);
  } else if (dataList.length === authVariable.auth.length) {
    authVariable.setIntermin(false);
    authVariable.setCheckAll(true);
  }
};

export const checkLen = (...arrs: Auths[][]) => {
  for (const arr of arrs) {
    if (arr.length !== 0) {
      return true;
    }
  }
  return false;
};
